//detail.js
//获取应用实例
const app = getApp()
const util = require('../../utils/util.js')

Page({
  data: {
    invoiceId: '',
    invoice: {
      invoiceName: '深圳电子普通发票', //发票名称
      purchaseName: '深圳市上佳科技有限公司', //购方名字
      sellerName: '深圳市伟岩餐饮有限公司', //销售方名字
      amount: '223', //合计金额
      invoiceDate: '2020-01-01', //开票日期
      sourceStatusName: '餐饮服务', //项目类别
      invoiceCode: '144032009110', //发票代码
      invoiceNumber: '06145006', //发票号码
    },
  },
  onShow: function () {
  },
  onLoad: function (option) {
    var that = this
    var invoiceId = option.invoiceId
    if (invoiceId != 0) {
      app.http.post('/wechat/findInvoice',{invoiceId: invoiceId}).then(res => {
        if (res.status == '200') {
          var invoice = res.data
          that.setData({
            invoiceId: invoiceId,
            invoice : invoice
          })
        }
      })
    } else {
      var invoice = {}
      invoice.invoiceName = '普通电子发票'
      invoice.purchaseName = '奥南可多建筑有限公司'
      invoice.sellerName = '深圳市上佳科技有限公司'
      invoice.amount = '240'
      invoice.invoiceDate = '2019-12-03'
      invoice.sourceStatusName = '建筑服务'
      invoice.invoiceCode = '044031900104'
      invoice.invoiceNumber = '12783065'
      that.setData({
        invoiceId: invoiceId,
        invoice : invoice
      })
    }
  },
  lookInvoice: function() { //查看pdf发票
    var that = this
    var invoiceId = that.data.invoiceId
    app.http.post('/wechat/getInvoiceFile',{invoiceId: invoiceId}).then(res => {
      if (res.status == '200') {
        var fileUrl = res.data.fileUrl
        if (fileUrl != null) {
          wx.downloadFile({ //下载文件
            url: app.http.host + fileUrl,
            success: function (res) {
              var filePath = res.tempFilePath
              console.log(filePath)
              wx.openDocument({
                filePath: filePath,
                fileType: 'pdf',
                success: function (res) {
                  console.log("打开文档成功")
                  console.log(res);
                },
                fail: function (res) {
                  console.log("fail");
                  console.log(res)
                },
              })
            }
          })
        } else {
          wx.showToast({
            title: '获取文件失败',
            icon: "none"
          })
        }
      } else {
        wx.showToast({
          title: '获取文件失败！',
          icon: "none"
        })
      }
    })
  }
})
